package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("strategies")
public class StrategyController{
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
   //关联明细
    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        //攻略阅读数+1
        strategyStatisVORedisService.viewnumIncrease(id,1);
        return JsonResult.success(strategy);
    }

    //攻略查看全部
    //qo//此时不回显宜贴MOdelattribu标签，因为不要
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }


    //查询主题
    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }

    //评论添加
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){

        //设置用户信息
        //comment.setCity(userInfo.getCity());

        //属性拷贝
        //参数1:源对象 参数2:目标对象
        //将源对象的属性拷贝到目标对象, 前提:属性名词必须一样才可以拷贝
        //实现原理:内省机制
        BeanUtils.copyProperties(userInfo, comment);
        //用户id无法拷贝, 属性不一样
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);


        //评论数+1
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(), 1);
        return JsonResult.success();
    }

    //评论分页
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    //评论点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid, userInfo.getId());
        return JsonResult.success();
    }

    //告诉当前是哪个攻略
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }
    //攻略收藏
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo){
        //true: 表示收藏操作, false: 表示取消收藏操作
        boolean  ret = strategyStatisVORedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }
    //攻略点赞
    @RequireLogin
    @PostMapping("/strategyThumbup")
    //用户id和攻略id做记号所以需要两个次数
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){
        //true: 表示顶成功操作, false: 表示今天顶过操作
        boolean  ret = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ret);
    }
}
